import Vuex from 'vuex'
import Vue from 'vue'
import router from '@/router'
import { LoginRequest, UserResRequest } from '@/api'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    // 城市定位
    CityName: null,
    // 组件切换
    showMe: true,
    // 当前城市
    NowCityOlny: JSON.parse(localStorage.getItem('NowCityOlny')) || 'AREA|19b39d4f-8a36-d6f8',
    // 定位城市ID
    CityId: null,
    // 遮罩层
    UserOverlay: false,
    // 登录状态
    LoginState: localStorage.getItem('LoginState') || false,
    // token值
    token: localStorage.getItem('token') || '',
    houseCode: localStorage.getItem('houseId') || '',
    Userlist: [
      { id: 1, text: '我的收藏', icom: 'star-o' },
      { id: 2, text: '我的出租', icom: 'wap-home-o' },
      { id: 3, text: '看房记录', icom: 'underway-o' },
      { id: 4, text: '成为房主', icom: 'idcard' },
      { id: 5, text: '个人资料', icom: 'manager-o' },
      { id: 6, text: '联系我们', icom: 'service-o' }
    ],
    BottomNav: [
      { id: 1, text: '首页', icom: 'wap-home-o', url: '/layout/home' },
      { id: 2, text: '找房', icom: 'search', url: '/layout/findroom' },
      { id: 3, text: '资讯', icom: 'newspaper-o', url: '/layout/info' },
      { id: 4, text: '我的', icom: 'user-circle-o', url: '/layout/profile' }
    ],
    HomePageNav: [
      { id: 1, text: '整租', icom: 'wap-home-o', url: '/layout/findroom' },
      { id: 2, text: '合租', icom: 'friends-o', url: '/layout/findroom' },
      { id: 3, text: '地图找房', icom: 'location-o', url: '/map' },
      { id: 4, text: '去出租', icom: 'home-o', url: '/rent/add' }
    ],
    // 下拉菜单配置项
    DownMenuList: [{ id: 1, text: '区域', icom: 'wap-home-o', url: '/layout/home' },
      { id: 2, text: '方式', icom: 'friends-o', url: '/layout/findroom' },
      { id: 3, text: '租金', icom: 'location-o', url: '/layout/info' },
      { id: 4, text: '筛选', icom: 'home-o', url: '/layout/profile' }
    ],
    // 下拉菜单列表内容
    columns: [
      // 第一列
      {
        values: ['区域', '地铁']
      },
      // 第二列
      {
        values: [],
        defaultIndex: 0
      },
      {
        values: [],
        defaultIndex: 0
      }
    ],
    // 租房方式
    Rentallist: [
      {
        values: ['不限', '整租', '合租']
      }
    ],
    // 金额
    Moneylist: [
      {
        values: ['1000-2000', '2000-3000', '3000-4000', '4000-5000', '5000-6000', '7000-8000']
      }
    ],
    // 筛选房屋列表
    RoomScreenObj: {
      // 下拉菜单查询房屋
      NewHouse: null,
      RentalMode: null,
      // 金额
      Money: null,
      // 户型
      roomType: null,
      // 朝向
      oriented: null,
      // 楼层
      floor: null,
      // 起始页数
      start: 1,
      // 结束页数
      end: 10
    },
    HouseIcon: [
      { name: '电视', class: 'iconfont icon-dianshi' },
      { name: '冰箱', class: 'iconfont icon-bingxiang' },
      { name: '洗衣机', class: 'iconfont icon-xiyiji' },
      { name: '空调', class: 'iconfont icon-kongtiao' },
      { name: '热水器', class: 'iconfont icon-haofangtuo400iconfont2reshuiqi' },
      { name: '床', class: 'iconfont icon-shafa' },
      { name: '暖气', class: 'iconfont icon-nuanqi-' },
      { name: '宽带', class: 'iconfont icon-wifi' },
      { name: '衣柜', class: 'iconfont icon-yigui' },
      { name: '天然气', class: 'iconfont icon-tianranqi-01' }
    ]

  },
  mutations: {
    // 将token存至本地
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },
    // 当前房屋ID
    houseId (state, houseId) {
      state.houseCode = houseId
      localStorage.setItem('houseId', houseId)
    },
    // 登录状态
    Login (state) {
      console.log(state.LoginState)
      localStorage.setItem('LoginState', state.LoginState)
    },
    // 提示弹出框
    dialog (state, { title, message, confirmButtonText, cancelButtonText, url, canurl, reload }) {
      Vue.prototype.$dialog.confirm({
        title,
        message,
        cancelButtonText,
        confirmButtonText
      })
        .then(() => {
          if (reload) {
            location.reload()
          }
          if (!url) return
          router.push({
            path: url
          })
        })
        .catch(() => {
          if (!canurl) return
          router.push({
            path: canurl
          })
        })
    },
    NowCityOnly (state) {
      localStorage.setItem('NowCityOlny', JSON.stringify(state.NowCityOlny))
    }
  },
  actions: {
    // 发起请求，获取用于token
    async LoginActions (store, val) {
      try {
        // 登录接口
        const res = await LoginRequest(val)
        if (res.data.body === null) return
        const { token } = res.data.body
        // token本地存储
        store.commit('setToken', token)
        // 登录状态本地存储
        store.commit('Login')
        return Promise.resolve(res) // 返回成功的Promise对象
      } catch (err) {
        return Promise.reject(err) // 返回失败的Promise对象
      }
    },
    // 判断用户登录状态
    async UserRes (store) {
      // 进入首页判断token是否过期，是否有
      const User = await UserResRequest()
      if (User.data.status !== 200) {
        // LoginState登录状态判断用户是否登录
        store.state.LoginState = false
        store.commit('Login')
      } else {
        store.state.LoginState = true
        store.commit('Login')
      }
    }
  },
  getters: {

  }
})
